﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace SingletonPattern
{
    class Singleton
    {
        private Singleton()
        {
            Thread.Sleep(1000);

            Console.WriteLine("{0} 被构造，线程{1}", this.GetType().Name, Thread.CurrentThread.ManagedThreadId);
        }

        private static Singleton _Singleton = null;
        private static object Singleton_Lock = new object();

        public static Singleton CreateInstance()
        {
            if (_Singleton == null)
            {
                Console.WriteLine("lock");

                lock (Singleton_Lock)
                {
                    //Thread.Sleep(1000);
                    if (_Singleton == null)
                    {
                        _Singleton = new Singleton();
                    }
                }
            }
            return _Singleton;
        }

        public void show()
        {
            Console.WriteLine("这里调用了{0}.show", this, GetType().Name);
        }
    }
}
